<template>
  <div class="app-container">
    <el-table
        ref="multipleTable"
        v-loading="loading"
        :data="tableData"
        tooltip-effect="dark"
        style="width: 100%"
        max-height="550px"
        border
    >
      <el-table-column prop="createTime" label="注册时间" width="180"/>
      <el-table-column prop="username" label="用户名" width="150"/>
      <el-table-column prop="nickName" label="昵称" width="150"/>
      <el-table-column prop="phoneNumber" label="电话号码" width="180"/>
      <el-table-column prop="email" label="邮箱"/>
      <el-table-column prop="gender" label="性别">
        <template v-slot="scope">
          {{ scope.row.gender || '' }}
        </template>
      </el-table-column>
      <el-table-column prop="type" label="用户角色">
        <template v-slot="scope">
          <el-tag v-if="scope.row.type === 'superAdmin'" type="warning" effect="dark">超级管理员</el-tag>
          <el-tag v-if="scope.row.type === 'commonAdmin'" type="warning" effect="light">商家</el-tag>
          <el-tag v-if="scope.row.type === 'commonUser'" type="info">普通用户</el-tag>
        </template>
      </el-table-column>
      <el-table-column label="操作">
        <template v-slot="scope">
          <el-button type="primary" size="mini" :disabled="scope.row.type==='superAdmin'" @click="handleEdit(scope.$index, scope.row)">
            修改信息
          </el-button>
          <el-button type="primary" style="margin-left: 5px;margin-top: 5px" size="mini" :disabled="scope.row.type==='superAdmin'" @click="updateAuth(scope.$index, scope.row)">
            修改权限
          </el-button>
          <el-button type="warning" style="margin-left: 0;margin-top: 5px" size="mini" :disabled="scope.row.type==='superAdmin'" @click="deleteUser(scope.$index, scope.row)">
            注销用户
          </el-button>
        </template>
      </el-table-column>
    </el-table>
    <el-dialog title="修改用户信息" :visible.sync="dialogFormVisible" :close-on-click-modal="false"
               :before-close="handleClose" width="30%" :destroy-on-close="true"
    >
      <el-form :model="addForm" ref="Form" :rules="formRules">
        <el-form-item label="昵称" prop="nickName">
          <el-input v-model="addForm.nickName" placeholder="请输入昵称" tabindex="2"></el-input>
        </el-form-item>
        <el-form-item label="电话号码" prop="phoneNumber">
          <el-input v-model="addForm.phoneNumber" placeholder="请输入电话号码" tabindex="2"></el-input>
        </el-form-item>
        <el-form-item label="邮箱" prop="email">
          <el-input v-model="addForm.email" placeholder="请输入邮箱" tabindex="2"></el-input>
        </el-form-item>
        <el-form-item label="性别">
          <el-radio-group v-model="addForm.gender">
            <el-radio label="男">男</el-radio>
            <el-radio label="女">女</el-radio>
          </el-radio-group>
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button @click="closeForm">取 消</el-button>
        <el-button type="primary" @click="sendForm">确 定</el-button>
      </div>
    </el-dialog>
  </div>
</template>

<script>
import { getUserList, updateUserProfile, deleteUser } from '@/api/user'
import { recordLog } from '@/api/recordLog'

export default {
  name: 'Users',
  data() {
    return {
      tableData: [],
      loading: false,
      addForm: {},
      dialogFormVisible: false,
      // 表单校验
      formRules: {
        nickName: [
          { required: true, message: '用户昵称不能为空', trigger: 'blur' }
        ],
        email: [
          { required: true, message: '邮箱地址不能为空', trigger: 'blur' },
          {
            type: 'email',
            message: '请输入正确的邮箱地址',
            trigger: ['blur', 'change']
          }
        ],
        phoneNumber: [
          { required: true, message: '手机号码不能为空', trigger: 'blur' },
          {
            pattern: /^1[3|4|5|6|7|8|9][0-9]\d{8}$/,
            message: '请输入正确的手机号码',
            trigger: 'blur'
          }
        ]
      },
      authForm: {}
    }
  },
  created() {
    this.fetchData()
  },
  methods: {
    deleteUser(index, row) {
      this.$confirm(`是否删除${row.username}?`, '提示', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning'
      }).then(() => {
        deleteUser(row.id).then(res => {
          recordLog({ username: this.$store.getters.username, operate: `管理员注销了用户${row.username}` })
          this.$message({
            type: 'success',
            message: '删除成功!'
          })
          this.fetchData()
        })
      })
    },
    handleEdit(index, row) {
      this.addForm = {
        username: row.username,
        id: row.id,
        nickName: row.nickName,
        phoneNumber: row.phoneNumber,
        email: row.email,
        gender: row.gender
      }
      this.dialogFormVisible = true
    },
    updateAuth(index, row) {
      const username = row.username
      const auth = row.type === 'commonUser' ? '商家' : '普通用户'
      this.$confirm(`是否更改用户${username}的权限为${auth}`, '提示', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning'
      }).then(() => {
        updateUserProfile({ id: row.id, type: row.type === 'commonUser' ? 'commonAdmin' : 'commonUser' }).then(res => {
          this.fetchData()
          this.$message({
            message: '修改成功',
            type: 'success',
            center: true
          })
          recordLog({ username: this.$store.getters.username, operate: `更改用户${username}权限为${auth}` })
        })
      })
    },
    handleClose(done) {
      this.addForm = {}
      done()
    },
    closeForm() {
      this.addForm = {}
      this.dialogFormVisible = false
    },
    fetchData() {
      this.loading = true
      getUserList().then(response => {
        this.tableData = response.data
        this.loading = false
      })
    },
    sendForm() {
      this.$refs.Form.validate(valid => {
        if (valid) {
          updateUserProfile(this.addForm).then(response => {
            recordLog({ username: this.$store.getters.username, operate: `修改用户${this.addForm.username}信息` })
            this.addForm = {}
            this.dialogFormVisible = false
            this.fetchData()
            this.$message({
              message: '修改成功',
              type: 'success',
              center: true
            })
          })
        } else {
          console.log('error submit!!')
          return false
        }
      })
    }
  }
}
</script>

<style scoped>
.el-button--primary.is-active,
.el-button--primary:active {
  background: #f79d36;
  border-color: #f79d36;
  color: #fff;
}

.el-button--primary:focus,
.el-button--primary:hover {
  background: #f8a03b;
  border-color: #f8a03b;
  color: #fff;
}

.el-button--primary {
  color: #FFF;
  background-color: #f79d36;
  border-color: #f79d36;
}

.el-button--warning.is-active,
.el-button--warning:active {
  background: #4d4c50;
  border-color: #4d4c50;
  color: #fff;
}

.el-button--warning:focus,
.el-button--warning:hover {
  background: #575559;
  border-color: #575559;
  color: #fff;
}

.el-button--warning {
  color: #FFF;
  background-color: #4d4c50;
  border-color: #4d4c50;
}
</style>
